from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.appiumby import AppiumBy
import time

caps = {
    "appActivity": "SplashActivity",
    "platformName": "Android",
    "platformVersion": "12",
    "deviceName": "127.0.0.1:16384",
    "appPackage": "com.sina.weibo"
}
url = "http://127.0.0.1:4723/wd/hub"
driver = webdriver.Remote(url, caps)
time.sleep(10)
# 点击同意按钮
driver.find_element(MobileBy.XPATH,
                    '//android.widget.TextView[@text="同意并继续"]').click()
time.sleep(10)
# 点击发现
TouchAction(driver).tap(x=544, y=1832).perform()
time.sleep(10)
# 点击我知道了
# driver.find_element(MobileBy.XPATH,
#                     "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.ImageView").click()
# time.sleep(10)
# 点击输⼊框
TouchAction(driver).tap(x=532, y=154).perform()
time.sleep(5)
# 输⼊框输⼊内容
driver.find_element(MobileBy.ID, "com.sina.weibo:id/tv_search_keyword").send_keys("8848")
time.sleep(5)
# 模拟键盘操作按下‘enter’键
driver.press_keycode(66)
time.sleep(5)
# 滑动⻚⾯5次
for i in range(5):
    TouchAction(driver).press(x=555, y=555).move_to(x=555, y=333).release().perform()
    time.sleep(5)
# 滑动⻚⾯5次(有过渡)
for i in range(5):
    driver.swipe(start_x=555, start_y=555, end_x=555, end_y=333, duration=800)
    time.sleep(2)  # 等待⻚⾯滚动完成
driver.quit()
